package com.lhy.vaccine_admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhy.vaccine_admin.entity.SAdmin;
import com.lhy.vaccine_admin.form.LoginForm;
import com.lhy.vaccine_admin.form.UserFrom;
import com.lhy.vaccine_admin.mapper.SAdminMapper;
import com.lhy.vaccine_admin.service.ISAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhy.vaccine_admin.vo.PageVo;
import com.lhy.vaccine_admin.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhy
 * @since 2023-02-22
 */
@Service
public class SAdminService extends ServiceImpl<SAdminMapper, SAdmin> implements ISAdminService {

    @Resource
    SAdminMapper sAdminMapper;

    /**
     * 实现登录
     * @param loginForm
     * @return 返回用户的姓名和手机号
     */
    @Override
    public R login(LoginForm loginForm) {
        //获取到用户名和密码
        SAdmin admin=sAdminMapper.getByAccount(loginForm.getSAccount(),loginForm.getSPassword());

        UserFrom userFrom = new UserFrom();

        // 验证是否查到用户
        if (admin != null){
            BeanUtils.copyProperties(admin, userFrom);
            return R.ok().put("msg",userFrom).put("data",200);
        }else{
            return R.error("没有该用户!").put("data",100);
        }
    }

    @Override
    public R getSearchByKey(String newValue) {
        List<String> stringList = null;
        if (newValue.equals("v_name")) {
            stringList = sAdminMapper.getSearchByName(newValue);
        }else{
            stringList = sAdminMapper.getSearchByKey(newValue);
        }
        if (stringList.size()>0 && stringList != null){
            return R.ok().put("result",stringList);
        }
        return R.error("没有查到数据!");
    }

    /**
     * 添加管理员
     * @param sAdmin
     * @return
     */
    @Override
    public R insertNewAdmin(SAdmin sAdmin) {
        int i = baseMapper.insert(sAdmin);
        if (i != 0){
            return R.ok().put("data","插入成功!");
        }
        return R.error();
    }

    /**
     * 删除管理员
     * @param sAdmin
     * @return
     */
    @Override
    public R deleteNewAdmin(SAdmin sAdmin) {
        int i = baseMapper.deleteById(sAdmin.getSId());
        if (i != 0){
            return R.ok().put("data","success");
        }
        return R.error();
    }

    @Override
    public R getAdminList(Integer currentPage, Integer pageSize) {
        Page<SAdmin> page = new Page<>(currentPage,pageSize);

        Page<SAdmin> adminPage = baseMapper.selectPage(page, null);

        PageVo pageVo = new PageVo();
        pageVo.setTotal(adminPage.getTotal());
        pageVo.setData(adminPage.getRecords());


        return R.ok().put("data",pageVo);
    }
}
